<?php
namespace Home\Controller;
use Think\Controller;
require './QiNiu'.'/autoload.php';
// require_once 'F:/codep/php/ypcang-api/api/vendor/autoload.php';
use Qiniu\Auth;

class UserController extends BaseController
{
  /**
   * 1.登录接口
   * 请求参数：
   * tel：手机号码
   * pwd: 密码
   *
  **/
  public function login() {
      $tel = I('tel'); //获取账户
      $pwd = I('pwd'); //获取密码
      // 判断参数是否为空
      $this->is_nullAction($tel);

      // 判断参数格式是否正确
      if (strlen($tel) <> 11) {
        $data['msg'] = '手机号位数不正确';
        $data['code'] = 1001;
        $this->error($data);
      }

      $this->is_nullAction($pwd);

      $m = M("users"); // 获取数据库表对象
      // $user = $m->where('mobile_number='.$tel)->field(array('uid', 'nickname'))->find(); // 用对象去根据条件查找对应的值
      $user = $m->where('mobile_number='.$tel)->find();

      if ($user == null) {
        $data['msg'] = '用户未注册';
        $data['code'] = 1001;
      	$this->error($data);
      }
      $password_hash = $user['password_hash'];
      if (strcasecmp($password_hash, $pwd) == 0) {
        $user['login_at'] = time();
        $m->save($user);
        $data['data'] = $user['token'];
        $data['msg'] = '登录成功';
        $this->success($data);
      }
      else {
        $data['msg'] = '密码错误';
        $data['code'] = 1001;
      	$this->error($data);
      }
  }
  /**
   * 2.注册接口
   * 请求参数：
   * tel：手机号码
   * pwd: 密码
   *
  **/
  public function register() {
    $tel = I('tel'); //获取账户
    $pwd = I('pwd'); //获取密码
    // 判断参数是否为空
    $this->is_nullAction($tel);
    // 判断参数格式是否正确
    if (strlen($tel) <> 11) {
      $data['msg'] = '手机号位数不正确';
      $data['code'] = 1001;
      $this->error($data);
    }
    $this->is_nullAction($pwd);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('mobile_number='.$tel)->find(); // 根据手机号查询该用户
    // 查到了就不能注册了
    if ($user == null) {
      $token = get_token($param="");
      $map['token'] = $token;
      $map['mobile_number'] = $tel;
      $map['password_hash'] = $pwd;
      $map['created_at'] = time();
      $map['updated_at'] = time();
      $m->add($map);
      $data['data'] = $token;
      $data['msg'] = '注册成功';
      $this->success($data);
    } else {
      $data['msg'] = '该用户已经注册';
      $data['code'] = 1001;
      $this->error($data);
    }
  }
  /**
   * 3.获取验证码
   * 请求参数：
   * tel：手机号码
   *
  **/
  public function getVerifyCode() {
    $tel = I('tel'); //获取账户
    // 判断参数是否为空
    $this->is_nullAction($tel);
    // 判断参数格式是否正确
    if (strlen($tel) <> 11) {
      $data['msg'] = '手机号位数不正确';
      $data['code'] = 1001;
      $this->error($data);
    }
    $verifyCode = rand_string(4, 1);
    $verifyStr = $verifyCode.'（动态验证码），请在5分钟内填写，请勿泄漏。【全是瑜】';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/send.json");

    curl_setopt($ch, CURLOPT_HTTP_VERSION  , CURL_HTTP_VERSION_1_0 );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);

    curl_setopt($ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD  , 'api:key-72642dc5ea36bfe1969fbe27863fe7b3');

    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $tel,'message' => $verifyStr));
    $res = json_decode(curl_exec($ch), true);
    curl_close($ch);
    $errorCode = $res['error'];
    if ($errorCode == '-40') {
        $data['msg'] = '手机号不正确';
        $this->error($data);
    }
    else if ($errorCode == '-20') {
        $data['msg'] = '余额不足';
        $this->error($data);
    }
    elseif ($errorCode == '-10') {
        $data['msg'] = '验证信息失败';
        $this->error($data);
    }
    elseif ($errorCode == '-11') {
        $data['msg'] = '用户接口被禁用';
        $this->error($data);
    }
    else {
      $data['data'] = $res;
      $data['msg'] = '获取成功';
      $this->success($data);
    }
  }
  /**
   * 3.更改昵称
   * 请求参数：
   * token：用户token
   * nick_name: 昵称
   *
  **/
  function updateNickname() {
    $token = I('token'); // 获取Token
    $nick_name = I('nick_name'); // 获取Token
    $this->is_nullAction($token);
    $this->is_nullAction($nick_name);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['nickname'] = $nick_name;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }

  function updateRealname() {
    $token = I('token'); // 获取Token
    $real_name = I('real_name'); // 获取Token
    $this->is_nullAction($token);
    $this->is_nullAction($real_name);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['realname'] = $real_name;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }

  /**
   * 4.更改头像
   * 请求参数：
   * token：用户token
   * avater：头像地址url
   *
  **/
  function updateAvatar() {
    $token = I('token'); // 获取Token
    $avatar = I('avatar'); // 获取头像地址url
    $this->is_nullAction($token);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['avatar'] = $avatar;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }

  /**
   * 5.更改email
   * 请求参数：
   * token：用户token
   * email: 邮箱地址
   *
  **/
  function updateEmail() {
    $token = I('token'); // 获取Token
    $email = I('email'); // 获取email
    $this->is_nullAction($token);
    $this->is_nullAction($email);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['email'] = $email;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }

  /**
   * 6.更改性别
   * 请求参数：
   * token：用户token
   * sex: 性别
   *
  **/
  function updateSex() {
    $token = I('token'); // 获取Token
    $sex = I('sex'); // 获取email
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['sex'] = $sex;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }
  /**
   * 7.更改地区
   * 请求参数：
   * token：用户token
   * area: 地区
   *
  **/
  function updateArea() {
    $token = I('token'); // 获取Token
    $city = I('city'); // 获取城市
    $prov = I('prov'); // 获取城市
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['city'] = $city;
      $user['prov'] = $prov;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }
  /**
   * 7.更改个性签名
   * 请求参数：
   * token：用户token
   * signature: 个性签名
   *
  **/
  function updateSignature() {
    $token = I('token'); // 获取Token
    $signature = I('signature'); // 获取个性签名
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['signature'] = $signature;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }
  /**
   * 7.更改deviceToken
   * 请求参数：
   * token：用户token
   * signature: 个性签名
   *
  **/
  function updateDeviceToken() {
    $token = I('token'); // 获取Token
    $deviceToken = I('deviceToken'); // 获取个性签名
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['device_token'] = $deviceToken;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '修改成功';
      $this->success($data);
    }
  }
  /**
   * 8.绑定微信
   * 请求参数：
   * token：用户token
   * wx_id: 微信标识
   *
  **/
  function bindWx() {
    $token = I('token'); // 获取Token
    $wx_id = I('wx_id'); // 获取wx_id
    $this->is_nullAction($token);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['wx'] = $wx_id;
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '头像修改成功';
      $this->success($data);
    }
  }

  /**
   * 9.解绑微信
   * 请求参数：
   * token：用户token
   *
  **/
  function unBindWx() {
    $token = I('token'); // 获取Token
    $this->is_nullAction($token);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['wx'] = "";
      $user['updated_at'] = time();
      $m->save($user);
      $data['msg'] = '头像修改成功';
      $this->success($data);
    }
  }

  public function collect() {
    $collect_id = I('collectId');
    $token = I('token');
    $type = I('type');
    if ($collect_id == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少收藏Id';
      $this->error($data);
    }
    if ($type == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少收藏类型';
      $this->error($data);
    }
    // if (!($type == 1 || $type == 2 || $type == 3)) {
    //   $data['code'] = '1001';
    //   $data['msg'] = '收藏类型不正确';
    //   $this->error($data);
    // }
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $m = M('collect');
    $list = $m->where(array('collect_id' => $collect_id, 'uid' => $uid))->find();
    if ($list != null) {
      $data['code'] = '1001';
      $data['msg'] = '已经收藏过';
      $this->error($data);
    }
    $map['collect_id'] = $collect_id;
    $map['uid'] = $uid;
    $map['type'] = $type;
    if ($type == 2) {
      $map['des'] = '视频';
      $video = M('video');
      $videoModel = $video->where(array('id' => $collect_id))->find();
      $videoModel['likecount'] = $videoModel['likecount'] + 1;
      $video->save($videoModel);
    }
    else if ($type == 3) {
      $map['des'] = '商户';
    }
    else {
      $map['type'] = 1;
      $map['des'] = $type;
      $plot = M('wedding');
      $plotModel = $plot->where(array('id' => $collect_id))->find();
      $plotModel['collectors_count'] = $plotModel['collectors_count'] + 1;
      $plot->save($plotModel);
    }
    $m->add($map);
    $data['msg'] = '收藏成功';
    $this->success($data);
  }

  public function unCollect() {
    $collect_id = I('collectId');
    $token = I('token');
    $type = I('type');
    if ($collect_id == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少收藏Id';
      $this->error($data);
    }
    if ($type == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少收藏类型';
      $this->error($data);
    }
    // if (!($type == 1 || $type == 2 || $type == 3)) {
    //   $data['code'] = '1001';
    //   $data['msg'] = '收藏类型不正确';
    //   $this->error($data);
    // }
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $m = M('collect');
    $list = $m->where(array('collect_id' => $collect_id, 'uid' => $uid))->select();
    if (count($list) > 0) {
      if(!$m->where(array('collect_id' => $collect_id, 'uid' => $uid))->delete()) {
        $data['code'] = '1001';
        $data['msg'] = '取消收藏失败';
        $this->error($data);
      }
    }
    if ($type == 2) {
      $video = M('video');
      $videoModel = $video->where(array('id' => $collect_id))->find();
      $videoModel['likecount'] = $videoModel['likecount'] - 1;
      $video->save($videoModel);
    }
    else if ($type == 3) {
      $map['des'] = '商户';
    }
    else {
      $plot = M('wedding');
      $plotModel = $plot->where(array('id' => $collect_id))->find();
      $plotModel['collectors_count'] = $plotModel['collectors_count'] - 1;
      $plot->save($plotModel);
    }
    $data['msg'] = '取消收藏成功';
    $this->success($data);
  }

  public function zan() {
    $zan_id = I('zanId');
    $token = I('token');
    $type = I('type');
    if ($zan_id == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少点赞Id';
      $this->error($data);
    }
    if ($type == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少点赞类型';
      $this->error($data);
    }
    if (!($type == 1 || $type == 2 || $type == 3)) {
      $data['code'] = '1001';
      $data['msg'] = '点赞类型不正确';
      $this->error($data);
    }
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $m = M('zan');
    $list = $m->where(array('zan_id' => $zan_id, 'uid' => $uid))->select();
    if (count($list) > 0) {
      $data['code'] = '1001';
      $data['msg'] = '已经赞过';
      $this->error($data);
    }
    $map['zan_id'] = $zan_id;
    $map['uid'] = $uid;
    $map['type'] = $type;
    if ($type == 1) {
      $map['des'] = '策划';
    }
    else if ($type == 2) {
      $map['des'] = '视频';
    }
    else if ($type == 3) {
      $map['des'] = '灵感';
    }
    $m->add($map);
    $data['msg'] = '点赞成功';
    $this->success($data);
  }

  public function unZan() {
    $zan_id = I('zanId');
    $token = I('token');
    $type = I('type');
    if ($zan_id == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少点赞Id';
      $this->error($data);
    }
    if ($type == null) {
      $data['code'] = '1001';
      $data['msg'] = '缺少点赞类型';
      $this->error($data);
    }
    if (!($type == 1 || $type == 2 || $type == 3)) {
      $data['code'] = '1001';
      $data['msg'] = '点赞类型不正确';
      $this->error($data);
    }
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $m = M('zan');
    $list = $m->where(array('zan_id' => $zan_id, 'uid' => $uid))->select();
    if (count($list) > 0) {
      if(!$m->where(array('zan_id' => $zan_id, 'uid' => $uid))->delete()) {
        $data['code'] = '1001';
        $data['msg'] = '取消赞失败';
        $this->error($data);
      }
    }
    $data['msg'] = '取消赞成功';
    $this->success($data);
  }

  function caseCollect() {
    $token = I('token');
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $collect = M('collect');
    $collectList = $collect->where(array('uid' => $uid, 'type' => 1))->select();
    $wedding = M('wedding');
    $resArr = null;
    $cehuaArr = array('title' => '婚礼策划', 'data' => array());
    $siyiArr = array('title' => '婚礼司仪', 'data' => array());
    $sheyingArr = array('title' => '婚礼摄影', 'data' => array());
    $genzhuangArr = array('title' => '新人跟妆', 'data' => array());
    $huayiArr = array('title' => '花艺甜品', 'data' => array());
    $lifuArr = array('title' => '婚纱礼服', 'data' => array());
    $hunxieArr = array('title' => '婚鞋搭配', 'data' => array());
    $hunyanArr = array('title' => '婚宴酒店', 'data' => array());
    $hunjieArr = array('title' => '婚介三金', 'data' => array());

    for ($i=0; $i < count($collectList); $i++) {
      $tempModel = $collectList[$i];
      $resModel = $wedding->where(array('id' => $tempModel['collect_id']))->find();
      $resModel['is_collect'] = '1';

      switch ($tempModel['des']) {
        case '策划':
          $cehuaArr['data'][] = $resModel;
          break;
        case '婚礼司仪':
          $siyiArr['data'][] = $resModel;
          break;
        case '摄影':
          $sheyingArr['data'][] = $resModel;
          break;
        case '跟妆':
          $genzhuangArr['data'][] = $resModel;
          break;
        case '花艺甜品':
          $huayiArr['data'][] = $resModel;
          break;
        case '婚纱礼服':
          $lifuArr['data'][] = $resModel;
          break;
        case '婚鞋':
          $hunxieArr['data'][] = $resModel;
          break;
        case '婚宴酒店':
          $hunyanArr['data'][] = $resModel;
          break;
        case '婚介三金':
          $hunjieArr['data'][] = $resModel;
          break;

        default:
          break;
      }
    }
    $resArr[] = $cehuaArr;
    $resArr[] = $siyiArr;
    $resArr[] = $sheyingArr;
    $resArr[] = $genzhuangArr;
    $resArr[] = $huayiArr;
    $resArr[] = $lifuArr;
    $resArr[] = $hunxieArr;
    $resArr[] = $hunyanArr;
    $resArr[] = $hunjieArr;
    $data['data'] = $resArr;
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  function videoCollect() {
    $token = I('token');
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $collect = M('collect');
    $collectList = $collect->where(array('uid' => $uid, 'type' => 2))->select();
    $video = M('video');
    $resArr = null;
    for ($i=0; $i < count($collectList); $i++) {
      $tempModel = $collectList[$i];
      $videoModel = $video->where(array('id' => $tempModel['collect_id']))->find();
      $videoModel['is_collect'] = '1';
      $resArr[$i] = $videoModel;
    }
    $data['data'] = $resArr;
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  function merchantCollect() {
    $token = I('token');
    $this->is_nullAction($token);
    $uid = $this->getMemberID($token);
    if ($uid == null) {
      $data['code'] = '998';
      $data['msg'] = '请登录账户';
      $this->error($data);
    }
    $collect = M('collect');
    $collectList = $collect->where(array('uid' => $uid, 'type' => 3))->select();
    $merchant = M('merchant');
    $resArr = null;
    for ($i=0; $i < count($collectList); $i++) {
      $tempModel = $collectList[$i];
      $merchantModel = $merchant->where(array('id' => $tempModel['collect_id']))->find();
      $merchantModel['is_collect'] = '1';
      $resArr[$i] = $merchantModel;
    }
    $data['data'] = $resArr;
    $data['msg'] = '获取成功';
    $this->success($data);
  }

  /**
   * 10.获取个人信息
   * 请求参数：
   * token：用户token
   *
  **/
  function mineInfo() {
    $token = I('token'); // 获取Token
    $this->is_nullAction($token);

    $uid = $this->getMemberID($token);
    $m = M("users"); // 获取数据库表对象
    $user = $m->where('uid='.$uid)->field(array('uid', 'nickname', 'realname', 'city', 'prov', 'signature', 'sex', 'avatar', 'mobile_number', 'email', 'token', 'wx', 'device_token'))->find(); // 根据uid查询该用户
    if ($user == null) {
      $data['msg'] = '请登录账户';
      $data['code'] = 998;
      $this->error($data);
    }
    else {
      $user['qn_baseUrl'] = "http://ojmi9m9wp.bkt.clouddn.com/";
      $user['about_mine'] = "http://www.huataiyihe.com/";//"http://guojingfen.cn/lastwork/fenApi/User/about_me.html";
      $data['msg'] = '获取成功';
      $data['data'] = $user;
      $this->success($data);
    }
  }

  function qnToken() {
    $accessKey = "1LKFtpGOd7BfMusecxXoCVlK98sMpvH70XaW3ud6";
    $secretKey = "TiRs_KOQFI_Z75jIyicYVSlili0JHbmve70uPR5J";
	  $auth = new Auth($accessKey, $secretKey);
	  $bucket = 'guojingfen';
	  $upToken = $auth->uploadToken($bucket);
    if ($upToken == null) {
      $data['code'] = 1001;
      $this->error($data);
    }
    $data['data'] = $upToken;
    $data['msg'] = '获取成功';
    $this->success($data);
  }
}
?>
